package com.dhq.myapp.base

import android.content.Context
import android.content.pm.ActivityInfo
import android.graphics.Color
import android.graphics.Color.TRANSPARENT
import android.os.Build
import android.os.Bundle
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.LinearLayout
import androidx.annotation.NonNull
import androidx.appcompat.app.AppCompatActivity
import com.dhq.myapp.R
import com.dhq.myapp.util.HeaderUtil

//import com.dhq.baselibrary.R
//import com.dhq.baselibrary.permission.EasyLocationPermissions
//import com.dhq.baselibrary.permission.EasyPermissions
//import com.dhq.baselibrary.permission.PermissionCallbacks
//import com.dhq.baselibrary.presenter.MvpPresenter
//import com.dhq.baselibrary.util.HeaderUtil

/**
 * @author admin
 * @date 2017/10/26
 */
abstract class BaseActivity2 : AppCompatActivity() {

    companion object {
        protected const val RC_PERM = 123
        protected const val RC_PERM_LOCATION = 124
//        protected var reSting = R.string.ask_again //默认提示语句
    }

    private lateinit var headerUtil: HeaderUtil
//    private var mvpPresenterImls: MutableList<MvpPresenter>? = null
    private var mListener: CheckPermListener? = null
    private var mLocationListener: CheckPermListener? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        // 设置为竖屏
        if (requestedOrientation != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
        super.onCreate(savedInstanceState)

        val contentView = layoutInflater.inflate(R.layout.activity_base, null)
        setContentView(contentView)

        val contentLinear = findViewById<LinearLayout>(R.id.content_view)
        contentLinear.addView(
            View.inflate(this, getLayout(), null),
            LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)
        )

        headerUtil = HeaderUtil(this, contentView)

        initStatusBar()
        initializeData()

        onCreateActivity(savedInstanceState)
    }

    protected fun initStatusBar() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            val window = window
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS or WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION)
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
            window.statusBarColor = TRANSPARENT
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            // 设置透明状态栏,这样才能让 ContentView 向上
            window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        }
    }

//    fun addPresenter(presenterIml: MvpPresenter) {
//        if (mvpPresenterImls == null) {
//            mvpPresenterImls = ArrayList()
//        }
//        mvpPresenterImls!!.add(presenterIml)
//    }
//
//    fun cleanPresenter() {
//        if (mvpPresenterImls != null && mvpPresenterImls!!.isNotEmpty()) {
//            for (mvpPresenterIml in mvpPresenterImls!!) {
//                mvpPresenterIml.detachView(false)
//            }
//        }
//    }

    override fun onDestroy() {
        super.onDestroy()
//        cleanPresenter()
    }

    interface CheckPermListener {
        //权限通过后的回调方法
        fun superPermission()
    }

//    fun checkPermission(listener: CheckPermListener, resString: Int, vararg mPerms: String) {
//        mListener = listener
//        val list = ArrayList<String>()
//        for (item in mPerms) {
//            if (!EasyPermissions.hasPermissions(this, item)) {
//                list.add(item)
//            }
//        }
//        val length = list.size
//        if (length > 0) {
//            val perms = arrayOfNulls<String>(list.size)
//            for (i in 0 until length) {
//                perms[i] = list[i]
//            }
//            EasyPermissions.requestPermissions(this, getString(resString), RC_PERM, *perms)
//        } else {
//            mListener?.superPermission()
//        }
//    }

//    /**
//     * 请求后台定位权限
//     * @param listener
//     * @param resString
//     */
//    fun checkLocationPermission(listener: CheckPermListener, resString: Int) {
//        mLocationListener = listener
//        if (EasyPermissions.hasPermissions(this, EasyLocationPermissions.BACKGROUND_LOCATION)) {
//            mLocationListener?.superPermission()
//            return
//        }
//
//        EasyLocationPermissions.reqBackgroundPermissions(this, getString(resString), RC_PERM_LOCATION)
//    }

//    override fun onPermissionsGranted(requestCode: Int, perms: List<String>) {
//        //同意了某些权限可能不是全部
//    }
//
//    override fun onPermissionsAllGranted() {
//        mListener?.superPermission() //同意了全部权限的回调
//    }
//
//    override fun onPermissionsDenied(requestCode: Int, perms: List<String>) {
//        if (requestCode == RC_PERM_LOCATION) {
//            //表示是后台定位权限请求
//            EasyLocationPermissions.checkDeniedPermissionsNeverAskAgain(
//                this,
//                getString(R.string.perm_back_location), perms
//            )
//        } else {
//            EasyPermissions.checkDeniedPermissionsNeverAskAgain(
//                this,
//                getString(R.string.perm_tip),
//                R.string.setting, R.string.cancel, null, perms
//            )
//        }
//    }

    /**
     * 用户权限处理,
     * 如果全部获取, 则直接过.
     * 如果权限缺失, 则提示Dialog.
     *
     * @param requestCode  请求码
     * @param permissions  权限
     * @param grantResults 结果
     */
    override fun onRequestPermissionsResult(
        requestCode: Int,
        @NonNull permissions: Array<String>,
        @NonNull grantResults: IntArray
    ) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults)
//        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this)
    }

    /**
     * 隐藏软键盘
     */
    protected fun hideKeyBoard() {
        if (window.attributes.softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
            val inputManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            currentFocus?.let {
                inputManager.hideSoftInputFromWindow(
                    it.windowToken,
                    InputMethodManager.HIDE_NOT_ALWAYS
                )
            }
        }
    }

    fun getHeaderUtil(): HeaderUtil {
        return headerUtil
    }

    protected open fun onCreateActivity(savedInstanceState: Bundle?) {}

    protected abstract fun getLayout(): Int

    protected abstract fun initializeData()
}